/**
 * functions with a structure argument
 *
 * fireway
 * 2019年 09月 20日 星期五 07:13:36 CST
 */
#include <iostream>
#include <cmath>

// structure declarations
struct Polar
{
    double distance;      // distance from origin
    double angle;         // direction from origin
};
struct Rect
{
    double x;             // horizontal distance from origin
    double y;             // vertical distance from origin
};

// prototypes
Polar rect_to_polar(Rect xypos);
void show_polar(Polar dapos);

int main()
{
    using namespace std;
    Rect rplace;
    Polar pplace;

    cout << "Enter the x and y values: ";
    while (cin >> rplace.x >> rplace.y)  // slick use of cin
    {
        pplace = rect_to_polar(rplace);
        show_polar(pplace);
        cout << "Next two numbers (q to quit): ";
    }
    cout << "Done.\n";
    return 0;
}

// convert Rectangular to Polar coordinates
Polar rect_to_polar(Rect xypos)
{
    using namespace std;
    Polar answer;

    answer.distance = sqrt( xypos.x * xypos.x + xypos.y * xypos.y);
    answer.angle = atan2(xypos.y, xypos.x);
    return answer;      // returns a Polar structure
}

// show Polar coordinates, converting angle to degrees
// 请注意，形参的类型，将一个polar结构体传递给该函数时，
// 该结构的内容将复制到dapos结构体中，函数将随后使用该拷贝完成工作。
void show_polar(Polar dapos)
{
    using namespace std;
    const double rad_to_deg = 57.29577951;

    cout << "distance = " << dapos.distance;
    cout << ", angle = " << dapos.angle * rad_to_deg;
    cout << " degrees\n";
}

